package org.example.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.system.entity.Account;
import org.example.system.entity.Account;
import org.example.system.mapper.AccountMapper;
import org.example.system.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;

@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;

    /**
     * 按条件分页查询
     * 影响条件有角色名，等级
     * @param account
     * @return
     */
    @Override
    public IPage<Account> selectPage(Account account){
        LambdaQueryWrapper<Account> wrapper = new LambdaQueryWrapper<>();
        //条件
        if (StrUtil.isNotEmpty(account.getName())){
            wrapper.like(Account:: getName,account.getName());    //相似的游戏角色名（模糊查询）
        }
        if (StrUtil.isNotEmpty(String.valueOf(account.getLevel()))){
            wrapper.like(Account:: getLevel,account.getLevel());    //相似的等级（模糊查询）
        }
        IPage<Account> page = new Page<>();
        page.setCurrent(account.getPageNum());
        page.setSize(account.getPageSize());
        return accountMapper.selectPage(page,wrapper);
    }

    /**
     * 添加角色
     * @param account
     * @return
     */
    @Override
    public int add(Account account) {
        account.setUpdateTime(new Date());
        account.setNote("角色创建成功！");
        return accountMapper.insert(account);
    }

    /**
     * 修改
     * @param account
     * @return
     */
    @Override
    public int update(Account account) {
        account.setUpdateTime(new Date());
        account.setNote("修改角色数据");
        return accountMapper.updateById(account);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(Long[] ids) {
        return accountMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 根据用户id获取信息
     * @param userId
     * @return
     */
    @Override
    public Account getOne(Long userId) {
        LambdaQueryWrapper<Account> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Account::getUserId,userId);
        Account account = accountMapper.selectOne(wrapper);
        return account;
    }
}
